package com.wlzg.collectiontool.view

import android.content.Context
import android.util.Log
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.PositionPopupView
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.adapter.DaoluAdapter
import com.wlzg.collectiontool.adapter.XiangmuAdapter
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.PsDaolu
import com.wlzg.collectiontool.model.bean.PsGongcheng
import com.wlzg.collectiontool.model.bean.PsXiangmu
import com.wlzg.collectiontool.model.bean.PsXiaoqu
import kotlinx.android.synthetic.main.dia_dllist.view.*
import kotlinx.android.synthetic.main.dia_dllist.view.diss
import kotlinx.android.synthetic.main.dia_dllist.view.recycler
import kotlinx.android.synthetic.main.dia_dllist.view.ssxmmc
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class DaolulistDia(var mContext: Context, var mFragment: ManageFragment) :
    PositionPopupView(mContext),
    View.OnClickListener {
    private var dataList: ArrayList<PsDaolu?>? = ArrayList()
    private var mAdapter: DaoluAdapter = DaoluAdapter()
    private var dlPop: BasePopupView? = null
    private var dldia: NewDlDia? = null
    override fun getImplLayoutId(): Int {
        return R.layout.dia_dllist
    }

    override fun onCreate() {
        super.onCreate()
        diss.setOnClickListener(this)
        sousuo.setOnClickListener(this)
        initListener()
        initData()

    }
    private fun initPopDia(t: PsDaolu?){
        dldia = NewDlDia(context,mFragment, t,this)
        dlPop = XPopup.Builder(context).dismissOnTouchOutside(false)
            .borderRadius(15.0f)
            .asCustom(dldia)
    }

    private fun initListener() {
        mAdapter.addChildClickViewIds(
            R.id.delete,
            R.id.bianji
        )
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.delete->{
                    mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
                        withContext(Dispatchers.IO) {
                            dataList?.get(position)?.let {
                                AppDatabase.getInstance().userDao().deleteDl(
                                    it
                                )
                                dataList?.removeAt(position)
                            }
                        }
                        mAdapter.notifyDataSetChanged()
                        mFragment.ondelete(3,null)
                    }

                }
                R.id.bianji->{
                    initPopDia(dataList?.get(position))
                    if(!(dlPop?.isShow!!)){
                        dlPop?.show()
                    }


                }
            }

        }
    }
    fun onUpdate(){
        mAdapter.notifyDataSetChanged()
    }


    private fun initData() {
        recycler.adapter=mAdapter
        recycler.layoutManager=LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false)
        mAdapter.setNewData(dataList)
        mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
            withContext(Dispatchers.IO) {
               val list = AppDatabase.getInstance().userDao().getdlList() as ArrayList<PsDaolu?>?
                Log.d("jkll","size"+list?.size)
                list?.let { dataList?.addAll(it) }

            }
            mAdapter.notifyDataSetChanged()

        }
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.diss -> {
                dismiss()
            }
            R.id.sousuo -> {
                var name=ssxmmc.text.toString()
                mFragment.lifecycleScope.launch(context = Dispatchers.Main) {
                    withContext(Dispatchers.IO) {
                        val list = AppDatabase.getInstance().userDao().getdlListbyname(name) as ArrayList<PsDaolu?>?
                        Log.d("jkll", "size" + list?.size)
                        list?.let {
                            dataList?.clear()
                            dataList?.addAll(it) }

                    }
                    mAdapter.notifyDataSetChanged()

                }
            }
        }
    }
}